﻿using System;
using System.Diagnostics.Contracts;
using UnityEngine;
public class SmartDataBase<T> where T : IComparable, new()
{
    [SerializeField]
    protected T valueMin = default(T);
    [SerializeField]
    protected T valueMax = default(T);
    protected T valueCur = default(T);

    public bool IsFull()
    {
        return Cur.CompareTo(Max) == 0;
    }
    public bool IsZero()
    {
        return Cur.CompareTo(Min) == 0;
    }
    public T Min
    {
        get => valueMin;
        set => valueMin = value;
    }

    public T Max
    {
        get => valueMax;
        set => valueMax = value;
    }

    public T Cur
    {
        get => valueCur;
        set => valueCur = value;
    }

    public SmartDataBase()
    {
        // 初始化默认值
        valueMin = default(T);
        valueMax = default(T);
        valueCur = default(T);
    }


    /// <summary>
    /// 构造函数，用于初始化 valueMin 和 valueMax。
    /// </summary>
    /// <param name="min">最小值</param>
    /// <param name="max">最大值</param>
    /// <exception cref="ArgumentException">当 min 大于 max 或为 null 时抛出</exception>
    public SmartDataBase(T min, T max)
    {
        if (min == null || max == null)
        {
            throw new ArgumentNullException("参数不能为 null");
        }
        if (min.CompareTo(max) > 0)
        {
            throw new ArgumentException("valueMin 不能大于 valueMax");
        }
        valueMin = min;
        valueMax = max;
        valueCur = min;
    }

    /// <summary>
    /// 生成一个在 [valueMin, valueMax] 范围内的随机值。
    /// </summary>
    /// <returns>随机生成的值</returns>
    public T Random()
    {
        Contract.Requires(valueMin != null && valueMax != null);
        try
        {
            return RandomTools.RandomRange<T>(valueMin, valueMax);
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine($"Random 方法发生异常: {ex.Message}");
            throw;
        }
    }

    public void DoReset()
    {
        valueCur = valueMin;
    }
    public void DoReset(T min, T max)
    {
        Min = min;
        Max = max;
    }
    public virtual void DoReset(T max)
    {
        Max = max;
        Cur = valueMin;
    }



    public virtual bool DoUpdate(float fDelta)
    {
        return false;
    }

    public virtual void DeepCopy(SmartDataBase<T> data)
    {
        Cur = data.Cur;
        Max = data.Max;
        Min = data.Min;
    }
}

[Serializable]
/// <summary>
/// 区间描述
/// </summary>
public class SmartSection : SmartDataBase<float>
{
    public SmartSection()
    {

    }
    public SmartSection(float min, float max) : base(min, max)
    {
        // 可扩展特定功能
    }

    public void DoInit(float min, float max)
    {
        Min = min;
        Max = max;
    }
}
